package com.hunlian.makelove.common;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.hunlian.makelove.net.NetTools;

public class NetworkStateBroadcastReciver extends BroadcastReceiver {
	public final static String ACTION_CHECK_NETWORK_STATE = "android.net.conn.CONNECTIVITY_CHANGE";
	
	private OnNetStateChangeListener mNetStateChangeListener;
	
	public void setDisconnectedListener(OnNetStateChangeListener netStateChangeListener){
		mNetStateChangeListener = netStateChangeListener;
	}
	
	public void onNetStateChanged(boolean netIsConnected){
		if(mNetStateChangeListener != null){
			mNetStateChangeListener.onNetStateChanged(netIsConnected);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Printout.println("action:"+action);
		if(action.equals(NetworkStateBroadcastReciver.ACTION_CHECK_NETWORK_STATE)){
			boolean netState = NetTools.isConnect(context);
			Printout.println("状态改变");
			onNetStateChanged(netState);
		}
	}

	public interface OnNetStateChangeListener{
		public void onNetStateChanged(boolean netIsConnected);
	}
}
